package com.example.environment.controller;

import com.alibaba.druid.util.StringUtils;
import com.example.environment.base.utils.BaseUuidUtil;
import com.example.environment.entity.reqvo.UserInfoVo;
import com.example.environment.service.UserService;
import com.example.environment.entity.User;
import com.example.environment.entity.base.BaseResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  @author: wangbo(1141294273@qq.com)
 *  @Date: 2021/1/29 15:47
 *  @Description: 登录
 */
@RestController
@RequestMapping("/api")
public class LoginController {
    private static Logger logger = LoggerFactory.getLogger(LoginController.class);


    @Autowired
    private UserService userService;



    /**
    * @Description  登录
    * @Author  wangbo
    * @Date   2021/1/29 17:54
    * @Param
    * @Return
    * @Exception
    *
    */
    @RequestMapping("/login")
    public BaseResponseBean login(@RequestBody UserInfoVo userInfoVo, HttpServletRequest request, HttpServletResponse response){

        User userInfo;
        try {
            if(StringUtils.isEmpty(userInfoVo.getUserName())){
                return new BaseResponseBean().isError("用户名不能为空!");
            }

            if(StringUtils.isEmpty(userInfoVo.getPassword())){
                return new BaseResponseBean().isError("密码不能为空!");
            }
            String uid = new BaseUuidUtil().getUuid("uid");
            User user = new User();
            user.setPassword(userInfoVo.getPassword());
            user.setName(userInfoVo.getUserName());
            userInfo = userService.login(user);
            User upUser = new User();
            upUser.setUid(uid);
            upUser.setId(userInfo.getId());

            if(null!= userInfo && userService.updateByUser(upUser)){
                //用户信息存入session
                HttpSession session = request.getSession();
                session.setAttribute(uid, userInfo);
                session.setMaxInactiveInterval(2 * 60);
                // 创建cookie并将成功登陆的用户保存在里面
                Cookie cookie = new Cookie("uid",uid);
                cookie.setMaxAge(-1);
                cookie.setPath("/");
                cookie.setHttpOnly(false);
                response.addCookie(cookie); // 服务器返回给浏览器cookie以便下次判断

                return new BaseResponseBean().isSuccess(true);
            }else{
                return new BaseResponseBean().isError("用户名密码错误");
            }
        } catch (Exception e) {
            logger.error("login登录方法异常",e);
            return new BaseResponseBean().isError("服务器异常,请联系管理员!");
        }

    }


    /**
    * @Description 登出
    * @Author  wangbo
    * @Date   2021/1/29 17:54
    * @Param
    * @Return
    * @Exception
    *
    */
    @RequestMapping("/outLogin")
    public BaseResponseBean outLogin(@RequestBody UserInfoVo userInfoVo,HttpServletRequest request,  HttpServletResponse response){
        try {
            String uid = userInfoVo.getUid();
            if(!StringUtils.isEmpty(uid)){
                //删除用户 session
                request.getSession().removeAttribute(uid);
                //清除cookie
                Cookie cookie = new Cookie("uid", null);
                cookie.setPath("/");
                cookie.setHttpOnly(false);
                cookie.setMaxAge(0); // 设置一分钟有效
                response.addCookie(cookie); // 服务器返回给浏览器cookie以便下次判断

                return  new BaseResponseBean().isSuccess(true);
            }
        } catch (Exception e) {
            logger.error("outLogin登出方法异常",e);
            return new BaseResponseBean().isError("服务器异,请常联系管理员!");
        }
        return new BaseResponseBean().isError("登出失败");

    }
}
